<?php
namespace app\api\service;
use think\Controller;
use think\Db;

class MemberAddress extends Controller {
    static protected $err_code = 0;
    // 获取用户收货地址列表接口
    static public function get_member_address($param) {
        if(!isset($param['member_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        try{
            $member_id = $param['member_id'];
            $member_address = Db::table('mob_member_address')->where('member_id', $member_id)->select();
            return ['data'=>$member_address,'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 获取用户收货地址详情接口
    static public function get_member_address_info($param) {
        if(!isset($param['member_id']) || !isset($param['address_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        try{
            $where['member_id'] = $param['member_id'];
            $where['id'] = $param['address_id'];
            $member_address_info = Db::table('mob_member_address')->where($where)->find();
            return ['data'=>$member_address_info,'code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 添加、编辑用户收货地址接口
    static public function save_member_address($param) {
        if(!isset($param['member_id']) || 
            !isset($param['address_id']) ||
            !isset($param['consignee']) ||
            !isset($param['phone']) ||
            !isset($param['province']) ||
            !isset($param['city']) ||
            !isset($param['area']) ||
            !isset($param['street']) ||
            !isset($param['address']) ||
            !isset($param['postcode']) ||
            !isset($param['is_default'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try{
            $data = [
                'consignee' => $param['consignee'],
                'phone' => $param['phone'],
                'province' => $param['province'],
                'city' => $param['city'],
                'area' => $param['area'],
                'street' => $param['street'],
                'address' => $param['address'],
                'postcode' => $param['postcode'],
                'is_default' => $param['is_default'],
                'add_time' => time()
            ];
            if ($address_id = $param['address_id'] > 0) {
                $where['member_id'] = $param['member_id'];
                $where['id'] = $param['address_id'];
                Db::table('mob_member_address')->where($where)->update($data);
            } else {
                $data['member_id'] = $param['member_id'];
                Db::table('mob_member_address')->insert($data);
            }
            Db::commit();
            return ['code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }

    // 设置用户默认收货地址接口
    static public function set_member_default_address($param) {
        if(!isset($param['member_id']) || !isset($param['address_id'])) return ['data'=>'','code'=>400001,'msg'=> 'FAULT'];

        // 启动事务
        Db::startTrans();
        try{
            Db::table('mob_member_address')->where('member_id', $param['member_id'])->update(['is_default' => 0]);
            Db::table('mob_member_address')->where('address_id', $param['address_id'])->update(['is_default' => 1]);
            // 提交事务
            Db::commit();
            return ['data'=>'','code'=>200,'msg'=> 'SUCCESS'];
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return ['code'=>self::$err_code, 'msg'=> $e->getMessage()];
        }
    }
}
